package com.gitee.cashzhang.test.basic.fore;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/04/23 16:45
 */
public class MapFore {

  public static void main(String[] args) {
    MapFore mapFore = new MapFore();

    Map<String, String> map = new HashMap<>();
    map.put("a", "a");
    map.put("b", "b");
    map.put("c", "c");

    mapFore.fore1(map);
    System.out.println("---");
    mapFore.fore2(map);
    System.out.println("---");
    mapFore.fore3(map);
    System.out.println("---");
    mapFore.fore4(map);
  }

  private void fore1(Map<String, String> map) {
    map.forEach((k, v) ->
        System.out.println(k + ":" + v)
    );
  }

  private void fore2(Map<String, String> map) {
    for (Entry entry : map.entrySet()) {
      System.out.println(entry.getKey() + ":" + entry.getValue());
    }
  }

  private void fore3(Map<String, String> map) {
    for (String key : map.keySet()) {
      System.out.println(key + ":" + map.get(key));
    }
  }

  private void fore4(Map<String, String> map) {
    Iterator<Entry<String, String>> entries = map.entrySet().iterator();
    while (entries.hasNext()) {
      Entry<String, String> entry = entries.next();
      System.out.println(entry.getKey() + ":" + entry.getValue());
    }
  }
}
